
class ProjectRepository {
  constructor(dao) {
    this.dao = dao
  }

  createTable() {
    const sql = `
          CREATE TABLE IF NOT EXISTS projects (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            fileList TEXT,
            fileIds TEXT,
            description TEXT,
            filesize INTEGER DEFAULT 0,
            timestamp INTEGER,
            expireTimestamp INTEGER,
            status INTEGER DEFAULT 1,
            download INTEGER DEFAULT 0,
            uv INTEGER DEFAULT 0,
            password TEXT
            )`
    return this.dao.run(sql)
  }

  create(task) {
    const { 
      fileList, 
      fileIds,
      description, 
      filesize, 
      timestamp,
      expireTimestamp,
      password
    } = task;

    return this.dao.run(
      `INSERT INTO projects (fileList, fileIds, description, filesize, timestamp, expireTimestamp, password)
        VALUES (?, ?, ?, ?, ?, ?, ?)`,
      [
        fileList,
        fileIds,
        description,
        filesize,
        timestamp,
        expireTimestamp,
        password
      ])
  }

  update(task) {
    const {
      status,
      id
    } = task
    return this.dao.run(
      `UPDATE projects 
        SET status = ? ,
        WHERE id = ?`,
      [status, id])
  }

  delete(id) {
    return this.dao.run(
      `DELETE FROM projects WHERE id = ?`,
      [id]
    )
  }

  getById(id) {
    return this.dao.get(
      `SELECT * FROM projects WHERE id = ?`,
      [id])
  }

  getAll() {
    return this.dao.all(`SELECT * FROM projects`)
  }
}

module.exports = ProjectRepository;